package com.lzx.iteam;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMsgHttpReceiver;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.util.Constants;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 12月 08日
 * Time: 上午11:11
 *
 * @QQ : 1234567890
 * **********************************
 *
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */

public class AttendanceFixProgressActivity extends MyBaseActivity implements View.OnClickListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.ll_layout_progress)
    private LinearLayout mLayoutProgress;
    @FindViewById(id = R.id.progress_at_fix)
    private ProgressBar mProgress;
    @FindViewById(id = R.id.tv_at_fix)
    private TextView mTvFix;

    private String mAttendanceId;
    private int mIndex = 0;
    private int mFixSucccess;

    private final int MSG_FIX = 30000;
    private final int MSG_PROGRESS = 30001;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            switch (msg.what){
                case MSG_FIX:
                    mHandler.sendEmptyMessage(MSG_PROGRESS);
                    if (msg.arg1 == 0){
                        try {
                            JSONObject object = (JSONObject) msg.obj;
                            JSONObject data = object.getJSONObject("data");
                            int ok = data.getInt("ok");
                            if (ok == 1){
                                mFixSucccess = 1;
                            }else{
                                mFixSucccess = 2;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else{
                        mFixSucccess = 0;
                        Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_LONG).show();
                    }
                    break;
                case MSG_PROGRESS:
                    mProgress.setProgress(mIndex);
                    mIndex += 2;
                    if (mIndex <= 96){
                        mHandler.sendEmptyMessageDelayed(MSG_PROGRESS,3);
                    }else{
                        upDateView();
                        mHandler.removeMessages(MSG_PROGRESS);
                    }
                    break;

            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.attendance_fix_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("在线修复");
        mLayoutProgress.setVisibility(View.VISIBLE);
        mAttendanceId = getIntent().getStringExtra("attendance_id");
        mTvFix.setText("正在处理...");
        fixData();
    }

    private void fixData(){
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_CAL_ID, mAttendanceId));
        GetMsgHttpReceiver msg = new GetMsgHttpReceiver(mHandler.obtainMessage(MSG_FIX));
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_FIX;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);

    }

    private void upDateView(){
        mProgress.setProgress(100);
        switch (mFixSucccess){
            case 0:
                mTvFix.setText("修复失败");
                break;
            case 1:
                mTvFix.setText("修复完成");
                Drawable drawable= getResources().getDrawable(R.drawable.iv_green_right);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                mTvFix.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft
                mTvFix.setCompoundDrawablePadding(Constants.dip2px(mActivity,4));//设置图片和text之间的间距
                break;
            case 2:
                mTvFix.setText("未发现问题\n如有疑问请进行意见反馈");
                break;
            default:
                mTvFix.setText("修复失败");
                break;

        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
        }
    }
}
